package com.example.lowbird.lowbird.fragment;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.animation.FastOutLinearInInterpolator;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.lowbird.lowbird.R;
import com.example.lowbird.lowbird.activity.LoginActivity;
import com.example.lowbird.lowbird.activity.MainActivity;
import com.example.lowbird.lowbird.activity.MyAddressActivity;
import com.example.lowbird.lowbird.activity.MyFavouriteActivity;
import com.example.lowbird.lowbird.activity.MylistActivity;
import com.example.lowbird.lowbird.activity.RegistActivity;
import com.example.lowbird.lowbird.bean.Logrbean;
import com.example.lowbird.lowbird.constant.Contants;
import com.example.lowbird.lowbird.utils.SpUtil;
import com.example.lowbird.lowbird.view.Mflist;
import com.google.gson.Gson;
import com.pkmmte.view.CircularImageView;

import org.w3c.dom.UserDataHandler;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * <p>
 * to handle interaction events.
 * Use the {@link MyFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MyFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private Context mContext;
    private Mflist mf_list;
    private CircularImageView iv_mflist;
    private TextView tv_mflist;
    private Button bt_getout;
    private Mflist mf_favorite;
    private Mflist mf_address;


    public MyFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MyFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MyFragment newInstance(String param1, String param2) {
        MyFragment fragment = new MyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

        mContext = getContext();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_my, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        mf_list = ((Mflist) view.findViewById(R.id.mf_list));
        iv_mflist = ((CircularImageView) view.findViewById(R.id.cv_myfragment));

        tv_mflist = ((TextView) view.findViewById(R.id.tv_myfragment));
        iv_mflist.setOnClickListener(listenser);
        tv_mflist.setOnClickListener(listenser);
        bt_getout = ((Button) view.findViewById(R.id.bt_getout));


        mf_address = ((Mflist) view.findViewById(R.id.mf_address));

        mf_favorite = ((Mflist) view.findViewById(R.id.mf_fav));

        initlistenser();
        String iflogin = SpUtil.getString(mContext, Contants.ifRLOGIN, "");
        if (!iflogin.equals("")) {

            String string = SpUtil.getString(mContext, iflogin, "");
            if (!string.equals("")) {

                Logrbean logrbean = new Gson().fromJson(string, Logrbean.class);
                Logrbean.DataBean data = logrbean.getData();
                if (data != null) {
                    bt_getout.setVisibility(View.VISIBLE);
                    tv_mflist.setText(data.getUsername());
                    Glide.with(mContext).load(data.getLogo_url()).diskCacheStrategy(DiskCacheStrategy.ALL)
                            .crossFade().into(iv_mflist);
                }

            }

        }

    }

    private void initlistenser() {

        bt_getout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SpUtil.putString(mContext, Contants.ifRLOGIN, "");
                Intent intent = new Intent(mContext, MainActivity.class);
                intent.putExtra("intent", "regist");
                startActivity(intent);


            }
        });


        mf_list.setIntent(new Intent(mContext, MylistActivity.class));

        mf_address.setIntent(new Intent(mContext, MyAddressActivity.class));
        mf_favorite.setIntent(new Intent(mContext, MyFavouriteActivity.class));
    }

    //监听事件  ，跳转到登录界面
    View.OnClickListener listenser = new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            startActivity(new Intent(mContext, RegistActivity.class));
        }
    };

}
